import "reflect-metadata";
import { createConnection, getRepository } from "typeorm";
//定义的实体类
import { User } from "./entity/User";
import { UserExtend } from './entity/UserExtend'
import { Posts } from './entity/Posts'
import { Tags } from './entity/Tags'
// 增
// createConnection().then(async connection => {
//     const post1 = new Posts()
//     post1.title = '礼品大放送2'
//     post1.content = '中秋礼品大放送xxxxxxxxx'

//     const post2 = new Posts()
//     post2.title = '国庆8天假1'
//     post2.content = '今年国庆假期加一天'

//     const user = new User();
//     user.username = "ldx3";
//     user.age = 29;
//     user.password = '23134'
//     user.isDel = 0
//     user.createTime = new Date();
//     //关联两个数据模型
//     user.posts = [post1,post2]

//     const postRepository = connection.getRepository(Posts)
//     await postRepository.save(post1)
//     await postRepository.save(post2)

//     const userRepository = connection.getRepository(User)
//     const result = await userRepository.save(user)
//     console.log(result);
// }).catch(error => console.log(error));

// //多对多--增加
// createConnection().then(async connection=>{
//     const tag1 = new Tags()
//     tag1.name = 'mysql2'
//     const tag2 = new Tags()
//     tag2.name = 'node2'

//     const post1 = new Posts()
//     post1.title = '和我一起学nestjs3'
//     post1.content = 'xxxxxxxxxxx'
//     post1.tags = [tag1,tag2]

//     const post2 = new Posts()
//     post2.title = '和我一起学node3'
//     post2.content = 'xxxw223sxxx'
//     post2.tags = [tag1]

//     const user = new User()
//     user.username = 'xyk2'
//     user.password = '1234'
//     user.posts = [post1,post2]

//     const userRepository = await connection.getRepository(User)
//     const postRepository = await connection.getRepository(Posts)
//     const tagRepository = await connection.getRepository(Tags)
//     await tagRepository.save(tag1)
//     await tagRepository.save(tag2)
//     await postRepository.save(post1)
//     await postRepository.save(post2)
//     await userRepository.save(user)
//     console.log('添加数据成功');
// })
// //查和删
// createConnection().then(async connection => {
//     // 使用relations关联查询数据(正向查找)
//     const userRepository = connection.getRepository(User)
//     //userDetail就是当前表中定义的字段
//     const result1 = await userRepository.find({ relations: ['posts'] })
//     console.log(result1);
//     // 使用relations关联查询数据(反向查找)
//     const postRepository = connection.getRepository(Posts)
//     // user就是当前表中定义的字段
//     const result2 = await postRepository.find({ relations: ['user'] })
//     console.log(result2);
// }).catch(error => console.log(error));
//查和删
createConnection().then(async connection => {
    const tagRepository = connection.getRepository(Tags)
    const result1 = await tagRepository.findOne({ where: { id: 7 }, relations: ['posts'] })
    console.log(result1);
    // const postsRepository = connection.getRepository(Posts);
    // const result = await postsRepository.findOne({ where: { id: 12 }, relations: ['tags', 'user'] });
    // console.log(result);
}).catch(error => console.log(error));

